package com.pro.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.RepaintManager;

import com.google.gson.Gson;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import com.pro.dao.impl.BannerDaoImpl;
import com.pro.dao.impl.UserDaoImpl;
import com.pro.entity.BannerEntity;
import com.pro.entity.ProductTypeEntity;
import com.pro.entity.UserEntity;
import com.pro.util.BaseServlet;
import com.pro.util.DateUtil;
import com.pro.util.Result;
import com.pro.util.ResultToLayui;

/**
 * Servlet implementation class TestServlet
 * @param <Gson>
 */
public class UserAdminServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	UserDaoImpl uDaoImpl = new UserDaoImpl();
	
	public void logo(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		
		  String name = request.getParameter("name");
		  String pwd = request.getParameter("pwd");
		  
		  UserEntity userEntity = uDaoImpl.login(name);
		  PrintWriter out = response.getWriter(); 
		  
		  if (userEntity==null) {
			out.print(Result.toClient("用户名错误", false));
		}else {
			if (userEntity.getPwd().equals(pwd)) {
				HttpSession session = request.getSession();
				session.setAttribute("userEntity", userEntity);
				out.print(Result.toClient("登录成功", true));
			}else {
				out.print(Result.toClient("密码错误", false));
			}
		}
		  
		  out.flush(); 
		  out.close(); 
	}
	
	public void logout(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		
		HttpSession session = request.getSession();
		session.invalidate();
		response.sendRedirect("admin/login.jsp");
	}
	
	public void test1(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		
		  ArrayList<UserEntity> aList = uDaoImpl.user();
		  PrintWriter out = response.getWriter(); 
		  out.print(ResultToLayui.toClient(0, "", aList.size(), aList));
		  out.flush(); 
		  out.close(); 
	}
	
	public void userUpdate(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		
		  int id= Integer.parseInt(request.getParameter("userId"));
		  String userName = request.getParameter("userName");
		  String pwd = request.getParameter("pwd"); 
		  int result = uDaoImpl.userUpdate(new UserEntity(id, DateUtil.DateToDate(new Date()), userName, pwd));
		  
		  PrintWriter out = response.getWriter(); 
		  out.print(Result.toClient("修改", result>0?true:false));
		  out.flush(); 
		  out.close(); 
	}
	
	
	  public void userAdd(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
	  
	  String userName = request.getParameter("userName");  
	  String pwd = request.getParameter("pwd"); 
	  int result = uDaoImpl.userAdd(new UserEntity(0, DateUtil.DateToDate(new Date()), userName, pwd));
	  
	  PrintWriter out = response.getWriter(); 
	  out.print(Result.toClient("添加", result>0?true:false)); 
	  out.flush(); 
	  out.close(); 
	  }
	 
	
	public void userDelete(HttpServletRequest request,HttpServletResponse response) throws Exception {
		int id= Integer.parseInt(request.getParameter("userId"));
		int result=uDaoImpl.userDelete(id);
		// 我们把数据输出到页面上
		PrintWriter printWriter = response.getWriter();
		printWriter.print(Result.toClient("删除",result>0?true:false));
		printWriter.flush();
		printWriter.close();
      }
}
